<?php
namespace App\Policies\Admin;

use App\Models\AdminUser;
use Illuminate\Database\Eloquent\Model;

class DdbUserPolicy
{
    public $model;
    protected $admin_user;
    protected $xref_exists = false;
    protected $is_super_manger;
    protected $configs;
    protected $end_time;
    public $only_view = ['3200503507', '3201502059', '2201301222', '1200306343', '1201800443', '2201000324', 'test008'];

    public function __construct(Model $model = null, AdminUser $admin_user = null)
    {
        $this->model = $model;
        $this->admin_user = $admin_user;
        $this->is_super_manger = $admin_user->isSuperManger();
        $this->end_time = "2023-12-01 00:00:00";
    }

    /**
     * 仅有查看的权限的管理员
     */
    public function only_can_view()
    {
        return in_array($this->admin_user->gid, $this->only_view);
    }

    /**
     * 结束之后，依然有编辑权限
     */
    public function over_can_edit()
    {
        return in_array($this->admin_user->gid, ['3201402268', '3201603816']);
    }

    public function view_all()
    {
        return $this->is_super_manger;
    }

    public function can_delete()
    {
        if ($this->only_can_view()) {
            return false;
        }

        // if ($this->over_can_edit()) {
        //     return true;
        // }

        if ($this->is_super_manger) {
            return true;
        }
        
        if (!$this->can_operate()) {
            return false;
        }
        $con1 = $this->admin_user->can($this->model->getTable());
        if (!$this->is_super_manger) {
            return $con1 && $this->model->dwdm == $this->admin_user->ddb_dwdm;
        }
        return $con1;
    }

    public function can_edit()
    {
        if ($this->only_can_view()) {
            return false;
        }

        if ($this->is_super_manger) {
            return true;
        }

        if (!$this->can_operate()) {
            return false;
        }
        if(is_object($this->model)){
            $con1 = $this->admin_user->can($this->model->getTable());
        }else{
            $con1 = false;
        }
        if (!$this->is_super_manger) {
            return $con1 && $this->admin_user->ddb_dwdm == $this->model->dwdm;
        }
        return $con1;
    }

    public function can_operate()
    {
        if ($this->only_can_view()) {
            return false;
        }
        return now()->toDateTimeString() <= $this->end_time && $this->admin_user->ddb_dwdm;
    }

    public function can_import()
    {
        if ($this->only_can_view()) {
            return false;
        }
        
        # 总管理员不可以导入
        if ($this->is_super_manger) {
            // return false;
        }
        return now()->toDateTimeString() <= $this->end_time;
    }

    public function can_download()
    {
        if ($this->is_super_manger) {
            return true;
        }elseif(is_object($this->model) && $this->admin_user->ddb_dwdm == $this->model->dwdm){
            return true;
        }
        return false;
    }

    public function can_export()
    {
        return $this->is_super_manger;
    }
    
    public static function make(...$params)
    {
        return new static(...$params);
    }
}